package com.lovo.csc.service.impl;

import com.lovo.csc.entity.Promotioninfo;
import com.lovo.csc.mapper.PromotioninfoMapper;
import com.lovo.csc.service.IPromotioninfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lovo.sscsys.dto.SellKillGoodsDto;
import com.lovo.util.MQUtil;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.stereotype.Service;

import java.io.IOException;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lovo
 * @since 2022-07-13
 */
@Service
public class PromotioninfoServiceImpl extends ServiceImpl<PromotioninfoMapper, Promotioninfo> implements IPromotioninfoService {
    @Autowired
    IPromotioninfoService promotionInfoService;

    @RabbitListener(queues = MQUtil.sellKillCheackQueue)
    public void savePromotionInfo(Channel channel, @Header(AmqpHeaders.DELIVERY_TAG) long tag, SellKillGoodsDto dto) throws IOException{
        if (dto!=null){
            Promotioninfo promotioninfo = new Promotioninfo();
            promotioninfo.setGoodname(dto.getGoodsName());
            promotioninfo.setGoodsort(dto.getGoodstype());
            promotioninfo.setPromotioninfo(dto.getInfo());
            promotioninfo.setPromotionstate("未审核");
            promotioninfo.setPurchaseprice(dto.getSellprice());
            promotioninfo.setGoodinfo(dto.getCode());
            promotionInfoService.save(promotioninfo);
        }
        channel.basicAck(tag,false);
    }

    @RabbitListener(queues = MQUtil.cancelCheackQueue)
    public void cancelPromotionInfo(Channel channel, @Header(AmqpHeaders.DELIVERY_TAG) long tag, SellKillGoodsDto dto) throws IOException, InterruptedException {
        if (dto != null){
            System.out.println(dto);
            Promotioninfo promotioninfo = new Promotioninfo();
            promotioninfo.setGoodname(dto.getGoodsName());
            promotioninfo.setGoodsort(dto.getGoodstype());
            promotioninfo.setPromotioninfo(dto.getInfo());
            promotioninfo.setPromotionstate("未审核");
            promotioninfo.setPurchaseprice(dto.getSellprice());
            promotioninfo.setGoodinfo(dto.getCode());
            promotionInfoService.save(promotioninfo);
        }
        channel.basicAck(tag,false);
    }

    @Override
    public void updateState(int PromotioninfoId, String state) {
        baseMapper.updateState(PromotioninfoId,state);
    }
}
